package ja12resttemplate;

import com.alibaba.fastjson2.JSONObject;
import ja12resttemplate.util.AssertUtil;
import ja12resttemplate.util.HttpClientUtil;
import ja12resttemplate.util.RestFulUtil;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class URLTest {

    public static final MediaType APPLICATION_JSON = new MediaType("application","json", StandardCharsets.UTF_8);
    public static final String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";

    public static void main(String[] args) throws Exception {
        System.out.println("第2部分：中化：沈阳化成用户同步");
        String url = "http://172.16.108.117:8080/imcpsei-infser/infserapi005/api/01?apikey=rY6OHHfqOqArk%2BqUeUn%2B4tJhNu82%2FYokDvmWol%2Bg1MzEHc9lfFLzYfsBtpKFXVcYDgu0yp3LkcNpquj%2B8z4Iyc5kJ9%2B9hXUXBoFvKX4fG%2FPRqRPAHnZ2mvRjbAdeKD%2B3nfJ7Ota6ILZspH%2Ff8mLtYe6qHwXKA9Fz%2FPWeW%2BQPvBA%3D";
        Map<String,Object> map1 = new HashMap<>(2);

        map1 = new HashMap<>(1);
        url = "http://localhost:8080/imcpsei-infser/infserapi005/api/01?apikey=rY6OHHfqOqArk%2BqUeUn%2B4tJhNu82%2FYokDvmWol%2Bg1MzEHc9lfFLzYfsBtpKFXVcYDgu0yp3LkcNpquj%2B8z4Iyc5kJ9%2B9hXUXBoFvKX4fG%2FPRqRPAHnZ2mvRjbAdeKD%2B3nfJ7Ota6ILZspH%2Ff8mLtYe6qHwXKA9Fz%2FPWeW%2BQPvBA%3D";
        map1.put("orgNo","91210122MA0QFQAK05_SYHC");
        System.out.println("常用post请求：RestTemplateUtil："+RestTemplateUtil.restCommon(url,map1));

        System.exit(2);

        //测试用户接口数据获取
        map1 = new HashMap<>(2);
        url = "http://172.16.95.236:9200/login";
        map1.put("username"  , "admin");
        map1.put("password"  , "869qnd8@Y2KhP#FT");
        JSONObject personJson = RestFulUtil.postForEntityHeadersJson(url,map1);
        System.out.println("得到的json查询结果为：【成功：token】"+(personJson==null?null:personJson.toString()));

        map1 = new HashMap<>();
        url = "http://localhost:8080/imcpsei-infser/infserapi005/api/01?apikey=rY6OHHfqOqArk%2BqUeUn%2B4tJhNu82%2FYokDvmWol%2Bg1MzEHc9lfFLzYfsBtpKFXVcYDgu0yp3LkcNpquj%2B8z4Iyc5kJ9%2B9hXUXBoFvKX4fG%2FPRqRPAHnZ2mvRjbAdeKD%2B3nfJ7Ota6ILZspH%2Ff8mLtYe6qHwXKA9Fz%2FPWeW%2BQPvBA%3D";
        map1.put("orgNo","91210122MA0QFQAK05_SYHC");
        JSONObject personJson1 = HttpClientUtil.post(url,map1);
        System.out.println("得到的json查询结果为【成功：httpClientUtil】："+(personJson1==null?null:personJson1.toString()));

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders2 = new HttpHeaders();
        httpHeaders2.setContentType(APPLICATION_JSON);
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map1, httpHeaders2);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,httpEntity,String.class);
        String jsonValue = responseEntity.getBody();
        System.out.println("得到的json查询结果为【失败1:postForEntity】："+(jsonValue==null?null:jsonValue.toString()));

        HttpHeaders httpHeaders3 = new HttpHeaders();
        httpHeaders3.add("Content-type", APPLICATION_JSON_UTF8_VALUE);
        HttpEntity<Map<String,Object>> request = new HttpEntity<>(map1,httpHeaders3);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST,request,String.class);
        String jsonValue3 = exchange.getBody();
        System.out.println("得到的json查询结果为：【失败2:exchange】"+(jsonValue3==null?null:jsonValue3.toString()));

        System.exit(1);
        System.out.println("第一部分：智慧工地的第三方接口");
        String body = "{\"username\":\"04cc5486f55e96f954b77015a4bf44d628342d3ecb49ae1ccfb727d62c44d6e650d6672cb21c02ba1caa911ed5baee646f67797b0a65b4866da9e65250426e78789a948cea9359673f97e0e66e1466394f81dff377b277bb5d5a71101a80fb9b493cf8cf840931dac6\",\"password\":\"0404a778442ff537726db2ee2537e31fad8e1748fdfe941c67b223291cdba96fc92f789abdbe2386f2217d044a7d2045557a3ffbdc08206b4dce832ce6dfc73279d3ef96eeda2f7715d926de6e50fb08e1ac1be076c5c30908067de6b9fe67ef7d8d0e574124dc783b95deadba\",\"code\":\"6\",\"uuid\":\"234c6d674b1a4cf08c0d599bb5ff8ffc\",\"loginUUID\":\"abc\"}";
        JSONObject map = JSONObject.parseObject(body, JSONObject.class);
        map.forEach((key,value)-> System.out.println("key:"+key+" value:"+value));
        System.exit(1);
        //测试参数拼接
        /*Map<String,Object> map = new HashMap<>();
        map.put("num",1.0);
        map.put("str","abc 1234");
        System.out.println(url4params("http://localhost:8888",map));*/
        //通过token，进行访问
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("prjNumber","a9cf6e55e4f5637c66159d7028a8e4c3979e6a6f");
        /*String url = "http://124.71.162.14:9005/labor-cloud-api/api/company/listByPrj";
        String url = "http://124.71.162.14:9005/labor-cloud-api/api/project/info";
        String url = "http://124.71.162.14:9005/labor-cloud-api/api/worker/listByPrj";*/
        String urlLabor = "http://124.71.162.14:9005/labor-cloud-api/api/attendance/listByPrj";
        paramMap.put("startDate", "2022-08-16");
        paramMap.put("endDate", "2022-08-19");
        JSONObject resultToken = RestFulUtil.getForEntity("http://124.71.162.14:9005/labor-cloud-api/api/account/getToken?account=bjhtnl&secret=fd3d36bf-1dfe-4015-95b9-207520566757");
        if(AssertUtil.isVal(resultToken)) {
            String token = resultToken.getString("data");
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("token",token);
            JSONObject result = RestFulUtil.getForEntityHeader(urlLabor,paramMap,httpHeaders);
            System.out.println(result.toJSONString());
        }

    }
}
